package com.bkhech.netty.httpwebsocket.pojo;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import lombok.Data;

/**
 * Response
 *
 * @author guowm
 * @date 2024-11-13
 */
@Data
public class Response<T> {
    private String msgId;
    private Integer code;
    private Integer respType;
    private String msgTime;
    private T data;
    private String errorMsg;

    private static final Integer SUCCESS_CODE = 200;

    public static <T> Response<T> success(String msgId, T data) {
        final Response<T> response = new Response<>();
        response.setMsgId(msgId);
        response.setCode(SUCCESS_CODE);
        response.setData(data);
        response.setRespType(RespType.RESPONSE.getType());
        response.setMsgTime(DateUtil.now());
        return response;
    }

    public static <T> Response<T> successNotify(String msgId, T data) {
        final Response<T> response = new Response<>();
        response.setMsgId(msgId);
        response.setCode(SUCCESS_CODE);
        response.setData(data);
        response.setRespType(RespType.NOTIFY.getType());
        response.setMsgTime(DateUtil.now());
        return response;
    }

    public static <T> Response<T> failure(String msgId, String errorMsg) {
        final Response<T> response = new Response<>();
        response.setMsgId(msgId);
        response.setCode(500);
        response.setErrorMsg(errorMsg);
        response.setRespType(RespType.RESPONSE.getType());
        response.setMsgTime(DateUtil.now());
        return response;
    }

    public String toJsonStr() {
        return JSONUtil.toJsonStr(this);
    }

}
